package com.eff.android.lib.connection;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.eff.android.api.model.Model;
import com.eff.android.api.model.ModelSubject;
import com.eff.android.config.EConfig;
import com.eff.android.config.EConstant;
import com.eff.android.lib.asynctask.JsonParser;
import com.eff.android.lib.helper.EDatabaseHelper;

public class EDataConnection {

	public final String TAG = EDataConnection.class.getSimpleName();
    
    // contacts JSONArray
    JSONArray dataJsonArr = null;
 		
 	private Context mContext;
 	
 	public EDataConnection(Context context) {
        super();
        mContext = context;
    }
    
    public String doInBackground() {

    	GetDataWord();
    	GetDataSubject();
    
        return null;
    }
    
    void GetDataWord() {
    	// Create List Model
        List<Model> listModels = new ArrayList<Model>();        
        
        try {           
            // instantiate our json parser
            JsonParser jParser = new JsonParser();                              
            
            // get json string from url
            dataJsonArr = jParser.getJSONFromUrl(EConfig.yourJsonStringUrl);
           
            // loop through all users
            for (int i = 0; i < dataJsonArr.length(); i++) {
            	
                JSONObject c = dataJsonArr.getJSONObject(i);                
                Model tempModel = CreateModelFromJSONObject(c);
                
                //Add Model to List
                listModels.add(tempModel);
                
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }               
    
        try {
            // Inserting models in database
                            	      	 
                    for (int i = 0; i < listModels.size(); i++)
                    {
                            
                        Log.e(TAG, "id_hinh: " + listModels.get(i).getIdHinh() 
	                        + ", van_ban: " + listModels.get(i).getVanBan()
	                        + ", y_nghia: " + listModels.get(i).getYNghia()
	                        + ", uri_hinh: " + listModels.get(i).getUriHinh()
	                        + ", phien_am_quoc_te: " + listModels.get(i).getPhienAmQuocTe()
	                        + ", loai_hinh: " + listModels.get(i).getLoaiHinh()
	                        + ", level: " + listModels.get(i).getLevel()
	                        + ", ma_chu_de: " + listModels.get(i).getMaChuDe()
	                        + ", date_create: " + listModels.get(i).getDateCreate());
                         EDatabaseHelper.getInstance(mContext).createModel(listModels.get(i));
                    }
            
        	
            Log.e("Model Count", "Model count: " + EDatabaseHelper.getInstance(mContext).getModelCount());
            
            } catch (SQLiteException e) {
            e.printStackTrace();
        }
    }
    
    private Model CreateModelFromJSONObject(JSONObject c) throws JSONException{
    	String id_hinh = c.getString(EConstant.KEY_ID_HINH);
        String van_ban = c.getString(EConstant.KEY_VAN_BAN);
        String y_nghia = c.getString(EConstant.KEY_Y_NGHIA);
        String uri_hinh = c.getString(EConstant.KEY_URI_HINH);
        String phien_am_quoc_te = c.getString(EConstant.KEY_PHIEN_AM_QUOC_TE);
        String loai_hinh = c.getString(EConstant.KEY_LOAI_HINH);
        String level = c.getString(EConstant.KEY_LEVEL);
        String ma_chu_de = c.getString(EConstant.KEY_MA_CHU_DE);               
        String date_create = c.getString(EConstant.KEY_DATE_CREATE);
        
     // Create Model
        Model tempModel = new Model(id_hinh, van_ban, y_nghia, uri_hinh, phien_am_quoc_te,
                        loai_hinh, level, ma_chu_de, date_create);
        
        return tempModel;
    }
    
    void GetDataSubject() {
    	
    	// Create Model Subject
    	List<ModelSubject> listModelsSubject = new ArrayList<ModelSubject>();
    	
    	try {
            
            // instantiate our json parser
            JsonParser jParser = new JsonParser();                              
            
            // get json string from url
            dataJsonArr = jParser.getJSONFromUrl(EConfig.yourJsonSubjectStringUrl);
           
            // loop through all users
            for (int i = 0; i < dataJsonArr.length(); i++) {

                JSONObject c = dataJsonArr.getJSONObject(i);

                // Storing each json item in variable
                String subjectId = c.getString(EConstant.KEY_ID_SUBJECT);
                String subjectName = c.getString(EConstant.KEY_NAME_SUBJECT);
                
                // Create Model Subject
                ModelSubject tempModel = new ModelSubject(subjectId, subjectName);
                
                //Add Model to List
                listModelsSubject.add(tempModel);                             
                
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }               
    
        try {
            // Inserting models in database
                            	      	 
                    for (int i = 0; i < listModelsSubject.size(); i++)
                    {
                            
                        Log.e(TAG, "subjectID: " + listModelsSubject.get(i).getSubjectID() 
	                        + ", subjectName: " + listModelsSubject.get(i).getSubjectName());
                         EDatabaseHelper.getInstance(mContext).createModelSubject(listModelsSubject.get(i));
                    }
            
            Log.e("Model Subject Count", "Model Subject count: " + EDatabaseHelper.getInstance(mContext).getModelSubjectCount());
                    
            // Don't forget to close database connection
            EDatabaseHelper.getInstance(mContext).closeDB();
            
            } catch (SQLiteException e) {
            e.printStackTrace();
        }
    }
}
